﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lottery.MediaData.Core {
    public static class DZXHelper {
        private static DZX GetLeft(List<DZX> data, int index) {
            DZX result = null;
            for (int i = index - 1; i >= 0; i--) {
                var item = data[i];
                if (item.Three == int.MinValue) continue;

                result = item;
                break;
            }

            if (result == null) {
                for (int i = data.Count - 1; i > index; i--) {
                    var item = data[i];
                    if (item.Three == int.MinValue) continue;

                    result = item;
                    break;
                }
            }

            return result;
        }

        private static DZX GetRight(List<DZX> data, int index) {
            DZX result = null;
            for (int i = index + 1; i < data.Count; i++) {
                var item = data[i];
                if (item.Three == int.MinValue) continue;

                result = item;
                break;
            }

            if (result == null) {
                for (int i = 0; i < index; i++) {
                    var item = data[i];
                    if (item.Three == int.MinValue) continue;

                    result = item;
                    break;
                }
            }

            return result;
        }

        public static CellData Gen(List<DZX> data) {
            var result = new CellData();

            // 清空以前的数据
            for (int col = 0; col < result.ColCount; col++)
                for (int row = 0; row < result.RowCount; row++)
                    result[col, row] = string.Empty;

            for (int i = 0; i < data.Count; i++) {
                var item = data[i];
                var col = i;

                result[col, 0] = item.D;
                result[col, 1] = item.Z;
                result[col, 2] = item.X;

                if (!string.IsNullOrEmpty(item.PreNumber)) {

                    // 处理 与预测号相同

                    if (item.D.IndexOf(item.PreNumber) != -1) {
                        result[col, 4] = item.D;
                    }

                    if (item.Z.IndexOf(item.PreNumber) != -1) {
                        result[col, 5] = item.Z;
                    }

                    if (item.X.IndexOf(item.PreNumber) != -1) {
                        result[col, 6] = item.X;
                    }

                    // 与预测号相同
                    for (int index = 4; index < 7; index++) {
                        result[col, 3] += result[col, index];
                    }
                }

                // 处理 与预测号相邻
                DZX left = GetLeft(data, i);
                DZX right = GetRight(data, i);

                var s = left.PreText;
                if (item.PreText != s) {
                    if (s == "大") {
                        result[col, 8] = item.D;
                    } else if (s == "中") {
                        result[col, 9] = item.Z;
                    } else if (s == "小") {
                        result[col, 10] = item.X;
                    }
                }

                s = right.PreText;
                if (item.PreText != s) {
                    if (s == "大") {
                        result[col, 8] = item.D;
                    } else if (s == "中") {
                        result[col, 9] = item.Z;
                    } else if (s == "小") {
                        result[col, 10] = item.X;
                    }
                }

                // 与预测号相邻
                string cellValue = string.Empty;
                for (int index = 8; index < 11; index++) {
                    cellValue += result[col, index];
                }
                result[col, 7] = cellValue.MySort();


                // 与预测号同＋邻
                for (int index = 0; index < 4; index++) {
                    var temp = result[col, 3 + index] + result[col, 7 + index];
                    result[col, 11 + index] = temp.MySort();
                }

            }

            // 计算场次合计
            for (int row = 0; row < result.RowCount; row++) {
                int count = 0;
                for (int col = 0; col < result.ColCount - 1; col++) {
                    if (result[col, row] != string.Empty)
                        count++;
                }

                result[14, row] = count.ToString();

            }

            return result;
        }
    }
}
